from langchain_core.messages import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
# from langchain.chat_models import ChatOpenAI
# from langchain_community.chat_models import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import os

from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import PromptTemplate
from pydantic import BaseModel, Field

# 提示词版本

os.environ["OPENAI_API_KEY"] = "sk-f5324346ba744ef89eda093af8f307c7"
os.environ["OPENAI_API_BASE"] = "https://dashscope.aliyuncs.com/compatible-mode/v1"
# os.environ["LANGCHAIN_VERBOSE"] = "false"
# os.environ["LANGCHAIN_TRACING_V2"] = "false"  # 小写false

# 移除所有LANGCHAIN_VERBOSE相关设置（已完全废弃）
# 添加新版SDK的日志控制配置
os.environ["LANGCHAIN_API_KEY"] = "ls_..."  # 非必填，仅需关闭日志时可不填
os.environ["LANGCHAIN_TRACING_V2"] = "false"


class Work(BaseModel):
    title: str = Field(description="Title of the work")
    description: str = Field(description="Description of the work")


parser = JsonOutputParser(pydantic_object=Work)

prompt = PromptTemplate(
    template="列举3部{author}的作品。\n{format_instructions}",
    input_variables=["author"],
    partial_variables={"format_instructions": parser.get_format_instructions()},
)

model = ChatOpenAI(model="deepseek-r1")

chain = prompt | model | parser
result = chain.invoke({"author": "老舍"})
print(result)




